var active=JSON.stringify(active);
localStorage.setItem("active",active);
var active=JSON.parse(localStorage.active);

// var operid = -1;
var totalPage;  //总页码
var currentPage=1; //当前页
var pageSize=4;


window.onload=function() {
    displayInfo();
    checkAll(); //添加全选事件
    checkItemAll();
    addPage();
    addPageEvent();
    // edit();   //修改
    addDept();


}

//搜索数据
$("#FindMess").click(function () {
    var isFalse=true;
    var messAge=$("#MessVul").val();
    // console.log(messAge);
    for(var i=0;i<active.length ;i++){
        //判断值是否相同 用学校搜索 或者活动名字
        if(messAge==active[i].schoolName){
            $("#tdCard").html("");    //清除数据

            $("#tdCard").append(`<tr>
                     <td><input type="checkbox" name="checkItem" id="checkItem"></td>
                     <td class="name">${active[i].schoolName}</td>
                     <td>${active[i].activeTime}</td>
                      <td class="activeId">${active[i].activeId}</td>
                     <td>${active[i].manager}</td>
                     <td>${active[i].phone}</td>
                     <td><button class="button_one" data-schoolId="${active[i].id}"  data-toggle="modal" data-target="#myLook">查看</button>
                     <button class="button_two"  data-index='"+i+"' data-toggle="modal" data-target="#myFix" >修改</button></td>
                 </tr>`)
            isFalse=false;
            break;
        }
        if (!isFalse){
            break;
        }
    }
    if (isFalse){

        $("#myMessWin").modal("show");

        $("#messOver").click(function (){
            $("#myMessWin").modal("hide");
        })

    }

})







//规定输入规范
//编号

$("#activeIdA").blur(function () {
    var activeNum=/(^00[1-9]$)|(^0[1-9][0-9]$)/;
    var activeTwo=$("#activeIdA").val();


    $("#activeIdA").parent().find("span").remove();


    for (var i=0;i<active.length;i++) {
        if(active[i].activeId==activeTwo){
            $("#activeIdA").parent().append("<span style='color:red' >编号重复请重新输入！</span>")
        }
    }

    if (!activeNum.test(activeTwo))  {
        $("#activeIdA").parent().append("<span style='color:red' >请输入正确的活动编号！</span>")
    }

})


//电话
$("#telA").blur(function () {


    var phoneNum=/^1[3|5|7|8|9]\d{9}$/;
    var textOne=$("#telA").val();
    var isFalse=false;

    // result=phoneNum.test(textOne);
    // console.log(result)
    $("#telA").parent().find("span").remove();

    //判断是否存在
    for (var i=0;i<active.length;i++) {
        if(active[i].phone==textOne){
            $("#telA").val("");    //清除数据
            $("#telA").parent().append("<span style='color:red' >电话已存在，请重新输入！</span>")
            isFalse=true;
            break;
        }
        break;

    }

    if (!isFalse){
        if (!phoneNum.test(textOne))  {
            $("#telA").parent().append("<span style='color:red' >输入不规范！</span>")
            isFalse=true;

        }

        else {
            isFalse=false;//输入正确

            //新增
            $("#AddNew").click(function(){




                // findActiveId();
                //添加数据  且获取表单数据
                var schoolName=$("#NameA").val();
                var activeName=$("#activeA").val();
                var activeId=$("#activeIdA").val();
                // console.log(activeId);
                var activeTime=$("#TimeA").val();
                var Address=$("#diDianA").val();
                var managerDept=$("#deptA").val();
                var manager=$("#ResManagerA").find("option:selected").text();
                var phone=$("#telA").val();

                var obj = {"schoolName":schoolName,"activeName":activeName,"activeId":activeId,"activeTime":activeTime,
                    "Address":Address,"managerDept":managerDept,"manager":manager,"phone":phone,};



                active.push(obj);


                $("#MyNew").modal("hide");
                // // 增加后再次获取页码
                displayInfo();
                addPage();
                //再一次清空值
                $("#NameA").val("");
                $("#activeA").val("");
                $("#activeIdA").val("");
                $("#TimeA").val("");
                $("#diDianA").val("");
                $("#deptA").val("");
                $("#ResManagerA").val("");
                $("#telA").val("");


            })
        }
    }




    /* if (!phoneNum.test(textOne))  {
         $("#telA").parent().append("<span style='color:red' >输入不规范！</span>")

     }*/
    // else {
    //
    // }
})


//部门方法活得
function addDept() {
    for(var i=0;i<staffDate.length ;i++){
        if(staffDate[i].depId=="003"){
            $("#ResManagerA").append(`<option>${staffDate[i].name}</option>`)
        }
    }
}





//查看
$("#tdCard").on("click",".button_one",function () {
    var schoolName=$(this).parent().parent().find(".name").text();
    // console.log(schoolName);
    for (var i=0;i<active.length ;i++){
        if(schoolName==active[i].schoolName){
            $("#Name").attr("disabled","disabled").val(active[i].schoolName);
            $("#active").attr("disabled","disabled").val(active[i].activeName);
            $("#Time").attr("disabled","disabled").val(active[i].activeTime);
            $("#diDian").attr("disabled","disabled").val(active[i].Address);
            $("#dept").attr("disabled","disabled").val(active[i].managerDept);
            $("#ResManager").attr("disabled","disabled").append(`<option selected>${active[i].manager}</option>`);
            $("#tel").attr("disabled","disabled").val(active[i].phone);
        }
    }
})


//修改
$("#tdCard").on("click",".button_two",function () {
    var activeId=$(this).parent().parent().find(".activeId").text();
    // console.log(activeId);
    for (var i=0;i<active.length ;i++){
        if(activeId==active[i].activeId){
            $("#NameB").val(active[i].schoolName);
            $("#activeB").val(active[i].activeName);
            $("#TimeB").val(active[i].activeTime);
            $("#diDianB").val(active[i].Address);
            $("#deptB").val(active[i].managerDept);
            $("#ResManagerB").append(`<option selected>${active[i].manager}</option>`);
            $("#telB").val(active[i].phone);
        }
    }

    $("#addFix").click(function () {
        // var index = $(this).attr();
        // console.log($("#NameB").val())       //获取修改的值
        // var activeId=$(".button_two").parent().parent().find(".activeId").text();

        for (var i=0;i<active.length ;i++) {    // 直接把修改的值替换原来的
            if (activeId == active[i].activeId) {
                // console.log(activeId);
                var  schoolName=$("#NameB").val();
                console.log(schoolName);
                var  activeName=$("#activeB").val();
                var  activeTime=$("#TimeB").val();
                var  Address=$("#diDianB").val();
                var  managerDept=$("#deptB").val();
                var  manager=$("#ResManagerB").find("option:selected").text();
                var  phone=$("#telB").val();

                active[i].schoolName=schoolName;
                active[i].activeName=activeName;
                active[i].activeTime=activeTime;
                active[i].Address=Address;
                active[i].managerDept=managerDept;
                active[i].manager=manager;
                active[i].phone=phone;

                displayInfo();
            }
        }
        $("#myFix").modal("hide");

        $("#myFixWin").modal("show");

        $("#addMyFixWin").click(function () {
            $("#myFixWin").modal("hide");
        })

    })



})






// 找到城市对应活动
$("#group").change(function () {
    $("#tdCard").html("");
    //继续获取select选中的值
    var city=$(this).find("option:selected").text();

    for(var i=0;i<shoolData.length;i++) {
        if (city == shoolData[i].shoolArea) {

            for (var j = 0; j < active.length; j++) {
                if(active[j].schoolId ==shoolData[i].shoolId){
                    $("#tdCard").append(`<tr>
                         <td><input type="checkbox" name="checkItem" id="checkItem"></td>
                         <td class="name">${active[j].schoolName}</td>
                         <td>${active[j].activeTime}</td>
                          <td class="activeId">${active[j].activeId}</td>
                         <td>${active[j].manager}</td>
                         <td>${active[j].phone}</td>
                         <td><button class="button_one" data-schoolId="${active[j].id}"  data-toggle="modal" data-target="#myLook">查看</button>
                         <button class="button_two"  data-index='"+i+"' data-toggle="modal" data-target="#myFix" >修改</button></td>
                     </tr>`)
                }
            }
        }


    }


})
//
//      //判断学校的城市
//     for(var i=0;i<active.length;i++){
//         if(city==active[i].schoolId){
//
//         }
//     }




//生成页码
function addPage(){
    var totalSize=active.length;
    totalPage=Math.ceil(totalSize/pageSize);  //总页数

    //每次生成总页数小于当前页 那要把当前页赋给总页码
    if(totalPage<currentPage){
        currentPage =totalPage ;
    }
    $("#divPage").html("");      //先清空
    for(var i=1;i<=totalPage;i++){
        $("#divPage").append(`<button class="btn btn-default">${i}</button>`);
    }

}
//再一次 添加页码的事件
function addPageEvent(){
    $("#divPage").on("click",".btn",function () {
        currentPage=Number ($(this).text());    //获取当前页的内容
        displayInfo();            //页面改变重新生成数据
    })
}


//全选
function checkAll(){
    $("#checkAll").click(function(){
        var checked = this.checked;
        $("input[name='checkItem']").each(function(index,item){
            item.checked =checked;
        })
    })
}
function checkItemAll(){
    $("#tdCard").on('click','input[name=checkItem]',function(){
        var isAll = true;
        $("input[name='checkItem']").each(function(index,item){
            if(!item.checked){
                isAll = false;
            }
        })
        if(isAll){
            $("#checkAll")[0].checked=true;
        }else{
            $("#checkAll")[0].checked=false;
        }
    })
}



//删除数据
$("#addDel").click(function () {
    $("tbody input").each(function (index,item) {
        // console.log(item);  //item找到的就是input
        if(item.checked==true){
            $(item).parent().parent().remove();

            // console.log($(item).parent().parent().find(".name"))

            var check=$(item).parent().parent().find(".activeId").text();
            // console.log(check)
            for (var i=0;i<active.length;i++) {
                if(active[i].activeId==check)

                    active.splice(i, 1);


            }
            // //关闭当前模态块
            $("#myDel").modal("hide");


            // //继续显示删除成功模态块
            $("#myDelWin").modal("show");

            $("#delOver").click(function () {
                $("#myDelWin").modal("hide");
            })
            // //点击确定 隐藏

            // // //删除后再次获取页码
            addPage();
            displayInfo();




        }
    })


})


//显示数据
function displayInfo(){
    $("#tdCard").html("");
    $("#checkAll")[0].checked=false;


    var start=(currentPage -1)*pageSize;
    var end=start+pageSize;
    //6条数据  两页  并且还要有active的长度 进去 所以要满足两个条件
    for(var i=start;i<end && i<active.length;i++){
        $("#tdCard").append(`<tr>
                     <td><input type="checkbox" name="checkItem" id="checkItem"></td>
                     <td class="name">${active[i].schoolName}</td>
                     <td>${active[i].activeTime}</td>
                      <td class="activeId">${active[i].activeId}</td>
                     <td>${active[i].manager}</td>
                     <td>${active[i].phone}</td>
                     <td><button class="button_one" data-schoolId="${active[i].id}"  data-toggle="modal" data-target="#myLook">查看</button>
                     <button class="button_two"  data-index='"+i+"' data-toggle="modal" data-target="#myFix" >修改</button></td>
                 </tr>`)

    }
}




